﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;

using XMK.Framework.DataAccess;
using XMK.Tadong.Model;
using XMK.Tadong.Model.Cond;
using XMK.Utils;

namespace XMK.Tadong.Data
{
    public class TB_Tree_ObjectData
    {
        protected DbProvider dbProvider;

        public TB_Tree_ObjectData(DbProvider dbProvider)
        {
            this.dbProvider = dbProvider;
        }

        /// <summary>
        /// 查询目标树或业务树数据
        /// </summary>
        /// <param name="treeType"></param>
        /// <returns></returns>
        public List<TB_Tree_ObjectInfo> QueryAllTreeObject(TreeType treeType = TreeType.Target)
        {
            List<TB_Tree_ObjectInfo> treeObjectList = new List<TB_Tree_ObjectInfo>();

            string sqlFormat = "select obj_id, obj_name, p_obj_id, obj_type, obj_type_desc, sn, isvalid, create_time, upd_time,SH_SQL from {0} order by sn asc";
            string sql = string.Empty;
            if (treeType == TreeType.Target) sql = string.Format(sqlFormat, "tb_tree_target");
            else if (treeType == TreeType.XMK) sql = string.Format(sqlFormat, "tb_tree_xmk");
            else
                sql = string.Format(sqlFormat, "tb_tree_bus");

            DbCmdHelper cmd = new DbCmdHelper(dbProvider, sql);

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            treeObjectList = DbManager.GetInfoList(cmd,
                delegate(out TB_Tree_ObjectInfo info, DataRow row)
                {
                    info = new TB_Tree_ObjectInfo
                    {
                        ObjectID = ParseHelper.Parse<int>(row["obj_id"]),
                        ObjectName = ParseHelper.Parse<string>(row["obj_name"]),
                        PObjectID = ParseHelper.Parse<int>(row["p_obj_id"]),
                        ObjectType = ParseHelper.Parse<int>(row["obj_type"]),
                        SN = ParseHelper.Parse<int>(row["sn"]),
                        ObjectTypeDesc = ParseHelper.Parse<string>(row["obj_type_desc"]),
                        SH_SQL = ParseHelper.Parse<string>(row["SH_SQL"]),
                        CreateTime = ParseHelper.Parse<string>(ParseHelper.Parse<DateTime>(row["create_time"]))
                    };
                });

            return treeObjectList;
        }
        

             /// <summary>
        /// 查询目标树或业务树数据
        /// </summary>
        /// <param name="treeType"></param>
        /// <returns></returns>
        public List<TB_Tree_ObjectInfo> QueryTreeObjectByPID(int P_OBJ_ID, TreeType treeType = TreeType.Target)
        {
            List<TB_Tree_ObjectInfo> treeObjectList = new List<TB_Tree_ObjectInfo>();

            string sqlFormat = "select obj_id, obj_name, p_obj_id, obj_type, obj_type_desc, sn, isvalid, create_time, upd_time from {0} where P_OBJ_ID =" + P_OBJ_ID.ToString() + " or P_OBJ_ID in(select obj_id from tb_tree_target where P_OBJ_ID =" + P_OBJ_ID.ToString() + ") order by sn asc";
            string sql = string.Empty;
            if (treeType == TreeType.Target) sql = string.Format(sqlFormat, "tb_tree_target");
            else if (treeType == TreeType.XMK) sql = string.Format(sqlFormat, "tb_tree_xmk");
            else
                sql = string.Format(sqlFormat, "tb_tree_bus");

            DbCmdHelper cmd = new DbCmdHelper(dbProvider, sql);
            string JH = "";
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            treeObjectList = DbManager.GetInfoList(cmd,
                delegate(out TB_Tree_ObjectInfo info, DataRow row)
                {
                    info = new TB_Tree_ObjectInfo
                    {
                        ObjectID = ParseHelper.Parse<int>(row["obj_id"]),
                        ObjectName = ParseHelper.Parse<string>(row["obj_name"]),//"<a href='www.baidu.com'>33</a>/<a href='www.baidu.com'>33</a>"
                        
                        PObjectID = ParseHelper.Parse<int>(row["p_obj_id"]),
                        ObjectType = ParseHelper.Parse<int>(row["obj_type"]),
                        SN = ParseHelper.Parse<int>(row["sn"]),
                        ObjectTypeDesc = ParseHelper.Parse<string>(row["obj_type_desc"]),
                        CreateTime = ParseHelper.Parse<string>(ParseHelper.Parse<DateTime>(row["create_time"]))
                    };
                });

            string ZJSql = @"select (select count(*) from AZ01 where jh='" + JH + "') +(select count(*) from AZ02 where jh='" + JH + "') +(select count(*) from AZ04 where jh='" + JH + "') ";
                   ZJSql +="(select count(*) from AZ05 where jh='英南3') +(select count(*) from AZ10 where jh='英南3') +(select count(*) from AZ13 where jh='英南3') ";
                   ZJSql +="(select count(*) from AZ14 where jh='英南3') +(select count(*) from AZ15 where jh='英南3') +(select count(*) from AZ16 where jh='英南3') ";
                   ZJSql +="(select count(*) from AZ17 where jh='英南3') +(select count(*) from AZ18 where jh='英南3') as DataCount,'钻井' as ProName  from dual ;";

            return treeObjectList;
        }

        /// 查询目标树或业务树数据
        /// </summary>
        /// <param name="treeType"></param>
        /// <returns></returns>
        public List<TB_Tree_ObjectInfo> QueryTreeObjectByPID1(int P_OBJ_ID, TreeType treeType = TreeType.Target)
        {
            List<TB_Tree_ObjectInfo> treeObjectList = new List<TB_Tree_ObjectInfo>();

            StringBuilder strSql_ = new StringBuilder();
            strSql_.Append("select obj_id, obj_name, p_obj_id, obj_type, obj_type_desc, sn, isvalid, create_time, upd_time from tb_tree_target where P_OBJ_ID =" + P_OBJ_ID.ToString() + " or P_OBJ_ID in(select obj_id from tb_tree_target where P_OBJ_ID =" + P_OBJ_ID.ToString() + ") order by sn asc");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql_.ToString());
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable dt = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            string JH = "";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TB_Tree_ObjectInfo info = new TB_Tree_ObjectInfo();
                info.ObjectID = int.Parse(dt.Rows[i]["obj_id"].ToString());
                info.ObjectName = dt.Rows[i]["obj_name"].ToString();
                JH = info.ObjectName;
                #region 井

                #region 钻井
                string ZJSql = @"select (select count(*) from AZ01 where jh='" + JH + "') +(select count(*) from AZ02 where jh='" + JH + "') +(select count(*) from AZ04 where jh='" + JH + "')+";
                ZJSql += "(select count(*) from AZ05 where jh='" + JH + "') +(select count(*) from AZ10 where jh='" + JH + "') +(select count(*) from AZ13 where jh='" + JH + "') +";
                ZJSql += "(select count(*) from AZ14 where jh='" + JH + "') +(select count(*) from AZ15 where jh='" + JH + "') +(select count(*) from AZ16 where jh='" + JH + "') +";
                ZJSql += "(select count(*) from AZ17 where jh='" + JH + "') +(select count(*) from AZ18 where jh='" + JH + "') as DataCount,'钻井' as ProName  from dual ";

                DbCmdHelper cmdZJ = new DbCmdHelper(dbProvider, ZJSql);
                cmdZJ.DbCmdInfo.CommandType = CommandType.Text;
                DataTable dtZJ = DbHelper.ExecFullDataTable(cmdZJ.DbProvider, cmdZJ.DbCmdInfo);
                string ZJCount = "";
                if (dtZJ.Rows.Count > 0)
                {
                    //钻井结构化数据
                    ZJCount = dtZJ.Rows[0]["DataCount"].ToString();
                }
                string ZJFSql = @"select count(*) as DataCount from tb_xmk_file where biz_obj like '%钻井%' and dest_obj='" + JH + "' and status !=0 and is_del=0";
                DbCmdHelper cmdZJF = new DbCmdHelper(dbProvider, ZJFSql);
                cmdZJF.DbCmdInfo.CommandType = CommandType.Text;
                DataTable dtZJF = DbHelper.ExecFullDataTable(cmdZJF.DbProvider, cmdZJF.DbCmdInfo);
                string ZJFCount = "";
                if (dtZJF.Rows.Count > 0)
                {
                    //钻井非结构化数据
                    ZJFCount = dtZJF.Rows[0]["DataCount"].ToString();
                }
                //钻井
                info.ZJCount = ZJFCount + "/" + ZJCount;
                #endregion 钻井
                #region 录井

                string LJSql = @"select (select count(*) from AZ06 where jh='" + JH + "') +(select count(*) from AZ07 where jh='" + JH + "') +(select count(*) from AZ19_1 where jh='" + JH + "')+";
                LJSql += "(select count(*) from AZ19_2 where jh='" + JH + "') +(select count(*) from AZ19_3 where jh='" + JH + "') as DataCount,'录井' as ProName  from dual";

                DbCmdHelper cmdLJ = new DbCmdHelper(dbProvider, LJSql);
                cmdLJ.DbCmdInfo.CommandType = CommandType.Text;
                DataTable dtLJ = DbHelper.ExecFullDataTable(cmdLJ.DbProvider, cmdLJ.DbCmdInfo);
                string LJCount = "";
                if (dtLJ.Rows.Count > 0)
                {
                    //录井结构化数据
                    LJCount = dtLJ.Rows[0]["DataCount"].ToString();
                }
                string LJFSql = @"select count(*) as DataCount from tb_xmk_file where biz_obj like '%录井%' and dest_obj='" + JH + "' and status !=0 and is_del=0";
                DbCmdHelper cmdLJF = new DbCmdHelper(dbProvider, LJFSql);
                cmdLJF.DbCmdInfo.CommandType = CommandType.Text;
                DataTable dtLJF = DbHelper.ExecFullDataTable(cmdLJF.DbProvider, cmdLJF.DbCmdInfo);
                string LJFCount = "";
                if (dtLJF.Rows.Count > 0)
                {
                    //录井非结构化数据
                    LJFCount = dtLJF.Rows[0]["DataCount"].ToString();
                }
                //录井
                info.LJCount = LJFCount + "/" + LJCount;

                #endregion 录井
                #region 测井

                string CJSql = @"select (select count(*) from AC01 where jh='" + JH + "') +(select count(*) from AC04 where jh='" + JH + "') +(select count(*) from AC04 where jh='" + JH + "')+";
                CJSql += "(select count(*) from AC06 where jh='" + JH + "') as DataCount,'录井' as ProName  from dual";

                DbCmdHelper cmdCJ = new DbCmdHelper(dbProvider, CJSql);
                cmdCJ.DbCmdInfo.CommandType = CommandType.Text;
                DataTable dtCJ = DbHelper.ExecFullDataTable(cmdCJ.DbProvider, cmdCJ.DbCmdInfo);
                string CJCount = "";
                if (dtCJ.Rows.Count > 0)
                {
                    //测井结构化数据
                    CJCount = dtCJ.Rows[0]["DataCount"].ToString();
                }
                string CJFSql = @"select count(*) as DataCount from tb_xmk_file where biz_obj like '%测井%' and dest_obj='" + JH + "' and status !=0 and is_del=0";
                DbCmdHelper cmdCJF = new DbCmdHelper(dbProvider, CJFSql);
                cmdCJF.DbCmdInfo.CommandType = CommandType.Text;
                DataTable dtCJF = DbHelper.ExecFullDataTable(cmdCJF.DbProvider, cmdCJF.DbCmdInfo);
                string CJFCount = "";
                if (dtCJF.Rows.Count > 0)
                {
                    //测井非结构化数据
                    CJFCount = dtCJF.Rows[0]["DataCount"].ToString();
                }
                //测井
                info.CJCount = CJFCount + "/" + CJCount;

                #endregion 测井
                #region 试油

                string SYSql = @"select (select count(*) from AS01 where jh='" + JH + "') +(select count(*) from AS02 where jh='" + JH + "') +(select count(*) from AS03 where jh='" + JH + "')+";
                SYSql += "(select count(*) from AS04 where jh='" + JH + "')+(select count(*) from AS06 where jh='" + JH + "')+(select count(*) from AS07 where jh='" + JH + "') +";
                SYSql += "(select count(*) from AS08 where jh='" + JH + "')+(select count(*) from AS09 where jh='" + JH + "') as DataCount,'试油' as ProName  from dual";

                DbCmdHelper cmdSY = new DbCmdHelper(dbProvider, SYSql);
                cmdSY.DbCmdInfo.CommandType = CommandType.Text;
                DataTable dtSY = DbHelper.ExecFullDataTable(cmdSY.DbProvider, cmdSY.DbCmdInfo);
                string SYCount = "";
                if (dtSY.Rows.Count > 0)
                {
                    //试油结构化数据
                    SYCount = dtSY.Rows[0]["DataCount"].ToString();
                }
                string SYFSql = @"select count(*) as DataCount from tb_xmk_file where biz_obj like '%试油%' and dest_obj='" + JH + "' and status !=0 and is_del=0";
                DbCmdHelper cmdSYF = new DbCmdHelper(dbProvider, SYFSql);
                cmdSYF.DbCmdInfo.CommandType = CommandType.Text;
                DataTable dtSYF = DbHelper.ExecFullDataTable(cmdSYF.DbProvider, cmdSYF.DbCmdInfo);
                string SYFCount = "";
                if (dtSYF.Rows.Count > 0)
                {
                    //试油非结构化数据
                    SYFCount = dtSYF.Rows[0]["DataCount"].ToString();
                }
                //试油
                info.SYCount = SYFCount + "/" + SYCount;

                #endregion 试油
                //#region 分析化验

                //string FXHYSql = @"select (select count(*) from AH01 where jh='" + JH + "') +(select count(*) from AH02 where jh='" + JH + "') +(select count(*) from AH03 where jh='" + JH + "')+";
                //FXHYSql += "(select count(*) from AH06 where jh='" + JH + "')+(select count(*) from AH06_F where jh='" + JH + "')+(select count(*) from AH07 where jh='" + JH + "') +";
                //FXHYSql += "(select count(*) from AH09 where jh='" + JH + "')+(select count(*) from AH09_1 where jh='" + JH + "') +(select count(*) from AH08 where jh='" + JH + "') +";
                //FXHYSql += "(select count(*) from AH10 where jh='" + JH + "')+(select count(*) from AH16 where jh='" + JH + "')+(select count(*) from AH18 where jh='" + JH + "') +";
                //FXHYSql += "(select count(*) from AH19 where jh='" + JH + "')+(select count(*) from AH21 where jh='" + JH + "') +(select count(*) from AH22 where jh='" + JH + "')+";

                //FXHYSql += "(select count(*) from AH23 where jh='" + JH + "')+(select count(*) from AH24 where jh='" + JH + "')+(select count(*) from AH26 where jh='" + JH + "') +";
                //FXHYSql += "(select count(*) from AH29 where jh='" + JH + "')+(select count(*) from AH11 where jh='" + JH + "') +(select count(*) from AH13 where jh='" + JH + "')+";
                //FXHYSql += "(select count(*) from AH14 where jh='" + JH + "')+(select count(*) from AH15 where jh='" + JH + "')+(select count(*) from AH27 where jh='" + JH + "') +";
                //FXHYSql += "(select count(*) from AH30 where jh='" + JH + "')+(select count(*) from AH31 where jh='" + JH + "') as DataCount,'分析化验' as ProName  from dual";
                
                //DbCmdHelper cmdFXHY = new DbCmdHelper(dbProvider, FXHYSql);
                //cmdFXHY.DbCmdInfo.CommandType = CommandType.Text;
                //DataTable dtFXHY = DbHelper.ExecFullDataTable(cmdFXHY.DbProvider, cmdFXHY.DbCmdInfo);
                //string FXHYCount = "";
                //if (dtFXHY.Rows.Count > 0)
                //{
                //    //分析化验结构化数据
                //    FXHYCount = dtFXHY.Rows[0]["DataCount"].ToString();
                //}
                //string FXHYFSql = @"select count(*) as DataCount from tb_xmk_file where biz_obj like '%分析化验%' and dest_obj='" + JH + "' and status !=0 and is_del=0";
                //DbCmdHelper cmdFXHYF = new DbCmdHelper(dbProvider, FXHYFSql);
                //cmdFXHYF.DbCmdInfo.CommandType = CommandType.Text;
                //DataTable dtFXHYF = DbHelper.ExecFullDataTable(cmdFXHYF.DbProvider, cmdFXHYF.DbCmdInfo);
                //string FXHYFCount = "";
                //if (dtFXHYF.Rows.Count > 0)
                //{
                //    //分析化验非结构化数据
                //    FXHYFCount = dtFXHYF.Rows[0]["DataCount"].ToString();
                //}
                ////分析化验
                //info.FXHYCount = FXHYFCount + "/" + FXHYCount;

                //#endregion 分析化验

                #region 其他



                #endregion 其他

                #region 无资产值
                if (i == 0)
                {
                    string WZCZSql = @"select count(*) as DataCount from tb_xmk_file where biz_obj is null and dest_obj is null and status !=0 and is_del=0";
                    DbCmdHelper cmdWZCZ = new DbCmdHelper(dbProvider, WZCZSql);
                    cmdWZCZ.DbCmdInfo.CommandType = CommandType.Text;
                    DataTable dtWZCZ = DbHelper.ExecFullDataTable(cmdWZCZ.DbProvider, cmdWZCZ.DbCmdInfo);
                    string WZCZCount = "";
                    if (dtWZCZ.Rows.Count > 0)
                    {
                        //分析化验非结构化数据
                        WZCZCount = dtWZCZ.Rows[0]["DataCount"].ToString();
                    }
                    //分析化验
                    info.WZCZCount = WZCZCount;
                }
                #endregion 无资产值

                #endregion 井

                info.PObjectID = int.Parse(dt.Rows[i]["p_obj_id"].ToString());
                info.ObjectType = int.Parse(dt.Rows[i]["obj_type"].ToString());
                info.SN = int.Parse(dt.Rows[i]["sn"].ToString());
                info.ObjectTypeDesc =dt.Rows[i]["obj_type_desc"].ToString();
                info.CreateTime = dt.Rows[i]["create_time"].ToString();
                treeObjectList.Add(info);
                    
            }

            
      

            return treeObjectList;
        }

        /// <summary>
        /// 通过关键词查询目标树或业务树数据
        /// </summary>
        /// <param name="treeType"></param>
        /// <returns></returns>
        public List<TB_Tree_ObjectInfo> QueryChildObjectByIdAndKeyWord(int id, string keyword, TreeType treeType = TreeType.Target)
        {
            List<TB_Tree_ObjectInfo> treeObjectList = new List<TB_Tree_ObjectInfo>();

            string sql = "select obj_id, obj_name, p_obj_id, obj_type, obj_type_desc, sn, isvalid, create_time, upd_time from {0} where P_OBJ_ID =" + id.ToString() + " and obj_name like '%" + keyword + "%'";
            if (treeType == TreeType.Target) sql = string.Format(sql, "tb_tree_target");
            else if (treeType == TreeType.XMK) sql = string.Format(sql, "tb_tree_xmk");


            DbCmdHelper cmd = new DbCmdHelper(dbProvider, sql);
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            treeObjectList = DbManager.GetInfoList(cmd,
                delegate(out TB_Tree_ObjectInfo info, DataRow row)
                {
                    info = new TB_Tree_ObjectInfo
                    {
                        ObjectID = ParseHelper.Parse<int>(row["obj_id"]),
                        ObjectName = ParseHelper.Parse<string>(row["obj_name"]),
                        PObjectID = ParseHelper.Parse<int>(row["p_obj_id"]),
                        ObjectType = ParseHelper.Parse<int>(row["obj_type"]),
                        SN = ParseHelper.Parse<int>(row["sn"]),
                        ObjectTypeDesc = ParseHelper.Parse<string>(row["obj_type_desc"]),
                        CreateTime = ParseHelper.Parse<string>(ParseHelper.Parse<DateTime>(row["create_time"]))
                    };
                });

            return treeObjectList;
        }


        /// <summary>
        /// 查询目标树或业务树数据
        /// </summary>
        /// <param name="treeType"></param>
        /// <returns></returns>
        public List<TB_Tree_ObjectInfo> QueryChildObjectById(int id, TreeType treeType = TreeType.Target)
        {
            List<TB_Tree_ObjectInfo> treeObjectList = new List<TB_Tree_ObjectInfo>();

            string sql = "select obj_id, obj_name, p_obj_id, obj_type, obj_type_desc, sn, isvalid, create_time, upd_time from {0} where P_OBJ_ID =" + id.ToString();
            if (treeType == TreeType.Target) sql = string.Format(sql, "tb_tree_target");
            else if (treeType == TreeType.XMK) sql = string.Format(sql, "tb_tree_xmk");


            DbCmdHelper cmd = new DbCmdHelper(dbProvider, sql);
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            treeObjectList = DbManager.GetInfoList(cmd,
                delegate(out TB_Tree_ObjectInfo info, DataRow row)
                {
                    info = new TB_Tree_ObjectInfo
                    {
                        ObjectID = ParseHelper.Parse<int>(row["obj_id"]),
                        ObjectName = ParseHelper.Parse<string>(row["obj_name"]),
                        PObjectID = ParseHelper.Parse<int>(row["p_obj_id"]),
                        ObjectType = ParseHelper.Parse<int>(row["obj_type"]),
                        SN = ParseHelper.Parse<int>(row["sn"]),
                        ObjectTypeDesc = ParseHelper.Parse<string>(row["obj_type_desc"]),
                        CreateTime = ParseHelper.Parse<string>(ParseHelper.Parse<DateTime>(row["create_time"]))
                    };
                });

            return treeObjectList;
        }

        /// <summary>
        /// 通过关键词查询目标树或业务树数据
        /// </summary>
        /// <param name="id">文件夹ID</param>
        /// <param name="personID">用户ID</param>
        /// <param name="TEAM_ID">课题组ID</param>
        /// <param name="treeType">查询类型</param>
        /// <param name="keyword">关键词</param>
        /// <returns></returns>
        public List<TB_Tree_ObjectInfo> PTQueryChildObjectByIdAndKeyWord(int id, string personID, string TEAM_ID, string treeType,string keyword)
        {
            List<TB_Tree_ObjectInfo> treeObjectList = new List<TB_Tree_ObjectInfo>();

            string sql = "";
            if (treeType == "PERSON")
            {
                sql = "select obj_id, obj_name, p_obj_id, obj_type, obj_type_desc, sn, isvalid, create_time, upd_time from TB_TREE_PERSON where P_OBJ_ID =" + id.ToString() + " and USER_ID='" + personID + "' and obj_name like '%" + keyword + "%'";
            }
            else if (treeType == "TEAM")
            {
                sql = "select obj_id, obj_name, p_obj_id, obj_type, obj_type_desc, sn, isvalid, create_time, upd_time from TB_TREE_TEAM where P_OBJ_ID =" + id.ToString() + " and  TEAM_ID=" + TEAM_ID + " and obj_name like '%" + keyword + "%'";
            }


            DbCmdHelper cmd = new DbCmdHelper(dbProvider, sql);
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            treeObjectList = DbManager.GetInfoList(cmd,
                delegate(out TB_Tree_ObjectInfo info, DataRow row)
                {
                    info = new TB_Tree_ObjectInfo
                    {
                        ObjectID = ParseHelper.Parse<int>(row["obj_id"]),
                        ObjectName = ParseHelper.Parse<string>(row["obj_name"]),
                        PObjectID = ParseHelper.Parse<int>(row["p_obj_id"]),
                        ObjectType = ParseHelper.Parse<int>(row["obj_type"]),
                        SN = ParseHelper.Parse<int>(row["sn"]),
                        ObjectTypeDesc = ParseHelper.Parse<string>(row["obj_type_desc"]),
                        CreateTime = ParseHelper.Parse<string>(ParseHelper.Parse<DateTime>(row["create_time"]))
                    };
                });

            return treeObjectList;
        }

        


        /// <summary>
        /// 查询目标树或业务树数据
        /// </summary>
        /// <param name="treeType"></param>
        /// <returns></returns>
        public List<TB_Tree_ObjectInfo> PTQueryChildObjectById(int id, string personID, string TEAM_ID, string treeType)
        {
            List<TB_Tree_ObjectInfo> treeObjectList = new List<TB_Tree_ObjectInfo>();

            string sql = "";//"select obj_id, obj_name, p_obj_id, obj_type, obj_type_desc, sn, isvalid, create_time, upd_time from {0} where P_OBJ_ID =" + id.ToString() + " and USER_ID=" + personID;
            if (treeType == "PERSON")
            {
                sql = "select obj_id, obj_name, p_obj_id, obj_type, obj_type_desc, sn, isvalid, create_time, upd_time from TB_TREE_PERSON where P_OBJ_ID =" + id.ToString() + " and USER_ID='" + personID + "'";
                //sql = string.Format(sql, "TB_TREE_PERSON");
            }
            else if (treeType == "TEAM")
            {
                sql = "select obj_id, obj_name, p_obj_id, obj_type, obj_type_desc, sn, isvalid, create_time, upd_time from TB_TREE_TEAM where P_OBJ_ID =" + id.ToString() + " and  TEAM_ID=" + TEAM_ID;
               // sql = "select obj_id, obj_name, p_obj_id, obj_type, obj_type_desc, sn, isvalid, create_time, upd_time from TB_TREE_TEAM where P_OBJ_ID =" + id.ToString() + " and USER_ID='" + personID + "' and TEAM_ID=" + TEAM_ID;
                //sql = string.Format(sql, "TB_TREE_TEAM");
            }


            DbCmdHelper cmd = new DbCmdHelper(dbProvider, sql);
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            treeObjectList = DbManager.GetInfoList(cmd,
                delegate(out TB_Tree_ObjectInfo info, DataRow row)
                {
                    info = new TB_Tree_ObjectInfo
                    {
                        ObjectID = ParseHelper.Parse<int>(row["obj_id"]),
                        ObjectName = ParseHelper.Parse<string>(row["obj_name"]),
                        PObjectID = ParseHelper.Parse<int>(row["p_obj_id"]),
                        ObjectType = ParseHelper.Parse<int>(row["obj_type"]),
                        SN = ParseHelper.Parse<int>(row["sn"]),
                        ObjectTypeDesc = ParseHelper.Parse<string>(row["obj_type_desc"]),
                        CreateTime = ParseHelper.Parse<string>(ParseHelper.Parse<DateTime>(row["create_time"]))
                    };
                });

            return treeObjectList;
        }



        public int getMaxObjSN(int p_id, TreeType treeType = TreeType.Target)
        {

            string sqlFormat = "select max(SN) from {0} where P_OBJ_ID =" + p_id.ToString();
            string sql = string.Empty;
            if (treeType == TreeType.Target) sql = string.Format(sqlFormat, "tb_tree_target");
            else if (treeType == TreeType.XMK) sql = string.Format(sqlFormat, "tb_tree_xmk");
            else
                sql = string.Format(sqlFormat, "tb_tree_bus");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, sql);
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            var maxSN = DbManager.ExecScalar(cmd);

            return Convert.IsDBNull(maxSN) ? 1 : Convert.ToInt32(maxSN);
        }

        public int getMaxObjId(TreeType treeType = TreeType.Target)
        {

            string sqlFormat = "select max(OBJ_ID) from {0}";
            string sql = string.Empty;
            if (treeType == TreeType.Target) sql = string.Format(sqlFormat, "tb_tree_target");
            else if (treeType == TreeType.XMK) sql = string.Format(sqlFormat, "tb_tree_xmk");
            else if (treeType == TreeType.TEAM) sql = string.Format(sqlFormat, "tb_tree_team");
            else if (treeType == TreeType.PERSON) sql = string.Format(sqlFormat, "tb_tree_person");
            else
                sql = string.Format(sqlFormat, "tb_tree_bus");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, sql);
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            var maxId = DbManager.ExecScalar(cmd);
            if (maxId.ToString() =="")
                return 0;
            return Convert.ToInt32(maxId);
        }

        /// <summary>
        /// treeObject实体
        /// </summary>
        /// <param name="cond"></param>
        /// <returns></returns>
        public TB_Tree_ObjectInfo GetTreeInfoByID(TB_Tree_ObjectCond cond)
        {

            string sqlFormat = "select obj_id,obj_name,p_obj_id,obj_type,obj_type_desc,create_time,SN,SH_SQL from {0} where obj_id =:p_ojbID ";
            string strSql = string.Empty;
            if (cond.desType == 0)
                strSql = string.Format(sqlFormat, "tb_tree_target");
            else if (cond.desType == 2)
                strSql = string.Format(sqlFormat, "tb_tree_xmk");
            else
                strSql = string.Format(sqlFormat, "tb_tree_bus");

            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString(), delegate
            {
                return DbCmdHelper.CreateParameters(dbProvider, "ojbID").ToArray();
            });
            cmd.SetParamValues(cond.ojbID);
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            TB_Tree_ObjectInfo treeinfo = DbManager.GetInfo(cmd,
                delegate(out TB_Tree_ObjectInfo info, DataRow row)
                {
                    info = new TB_Tree_ObjectInfo
                    {
                        ObjectID = ParseHelper.Parse<int>(row["obj_id"]),
                        ObjectName = ParseHelper.Parse<string>(row["obj_name"]),
                        PObjectID = ParseHelper.Parse<int>(row["p_obj_id"]),
                        ObjectType = ParseHelper.Parse<int>(row["obj_type"]),
                        ObjectTypeDesc = ParseHelper.Parse<string>(row["obj_type_desc"]),
                        SN = ParseHelper.Parse<int>(row["SN"]),
                        SH_SQL = ParseHelper.Parse<string>(row["SH_SQL"]),
                        CreateTime = ParseHelper.Parse<string>(ParseHelper.Parse<DateTime>(row["create_time"]))
                    };
                });
            return treeinfo;
        }


        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="cond"></param>
        /// <returns></returns>
        public bool EditTreeOjbect(TB_TreeObjectEditCond cond)
        {
            string tablename = "";
            if (cond.TypeID == 0)
                tablename = "tb_tree_target";
            else if (cond.TypeID == 2)
                tablename = "tb_tree_xmk";
            else
                tablename = "tb_tree_bus";
            StringBuilder strSql = new StringBuilder();
            //action:0(删除)
            if (cond.action == 0)
            {
                string _strsql = string.Format("delete from {0} WHERE OBJ_ID=:p_ojbID", tablename);
                DbCmdHelper cmd = new DbCmdHelper(dbProvider, _strsql,
                    delegate()
                    {
                        return DbCmdHelper.CreateParameters(dbProvider, "ojbID").ToArray();
                    });
                cmd.DbCmdInfo.CommandType = CommandType.Text;
                cmd.SetParamValues(cond.ObjectID);
                return DbManager.ExecNonQuery(cmd) > 0;
            }
            else if (cond.action == 1)
            {
                string _strsql = string.Format("update {0} set OBJ_NAME = :p_OBJ_NAME,P_OBJ_ID =:p_P_OBJ_ID,OBJ_TYPE = :p_OBJ_TYPE,OBJ_TYPE_DESC = :p_OBJ_TYPE_DESC,SN =:p_SN,ISVALID =:p_ISVALID,SH_SQL=:p_SH_SQL where OBJ_ID =:p_OBJ_ID", tablename);
                DbCmdHelper cmd = new DbCmdHelper(dbProvider, _strsql,
                    delegate()
                    {
                        return DbCmdHelper.CreateParameters(dbProvider, "OBJ_NAME", "P_OBJ_ID", "OBJ_TYPE", "OBJ_TYPE_DESC", "SN", "ISVALID", "OBJ_ID", "SH_SQL").ToArray();
                    });
                cmd.DbCmdInfo.CommandType = CommandType.Text;
                cmd.SetParamValues(cond.ObjectName, cond.PObjectID, cond.ObjectType, cond.ObjectTypeDesc, cond.SN, cond.IsValid, cond.ObjectID,cond.SH_SQL);
                return DbManager.ExecNonQuery(cmd) > 0;
            }
            else if (cond.action == 2)
            {
                if (IsExist(cond, "xmk"))
                {
                    return false;
                }
                strSql.Append("INSERT INTO " + tablename + " (OBJ_NAME ,P_OBJ_ID ,OBJ_TYPE ,OBJ_TYPE_DESC ,SN ,ISVALID,SH_SQL)");
                strSql.Append(" VALUES (:p_OBJ_NAME ,:p_P_OBJ_ID ,:p_OBJ_TYPE ,:p_OBJ_TYPE_DESC,:p_SN,:p_ISVALID,:p_SH_SQL)");
                DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString(),
                    delegate()
                    {
                        return DbCmdHelper.CreateParameters(dbProvider, "OBJ_NAME", "P_OBJ_ID", "OBJ_TYPE", "OBJ_TYPE_DESC", "SN", "ISVALID", "SH_SQL").ToArray();
                    });
                cmd.DbCmdInfo.CommandType = CommandType.Text;
                cmd.SetParamValues(cond.ObjectName, cond.PObjectID, cond.ObjectType, cond.ObjectTypeDesc, cond.SN, cond.IsValid,cond.SH_SQL);
                return DbManager.ExecNonQuery(cmd) > 0;
            }
            else if (cond.action == 3)//另外一个版本的插入
            {
                if (IsExist(cond, "xmk"))
                {
                    return false;
                }
                strSql.Append("INSERT INTO " + tablename + " (OBJ_NAME ,OBJ_ID,P_OBJ_ID ,OBJ_TYPE ,OBJ_TYPE_DESC ,SN ,ISVALID) ");
                strSql.Append(" VALUES (:p_OBJ_NAME ,:p_OBJ_ID,:p_P_OBJ_ID ,:p_OBJ_TYPE ,:p_OBJ_TYPE_DESC,:p_SN,:p_ISVALID)");
                DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString(),
                    delegate()
                    {
                        return DbCmdHelper.CreateParameters(dbProvider, "OBJ_NAME", "OBJ_ID", "P_OBJ_ID", "OBJ_TYPE", "OBJ_TYPE_DESC", "SN", "ISVALID").ToArray();
                    });
                cmd.DbCmdInfo.CommandType = CommandType.Text;
                cmd.SetParamValues(cond.ObjectName, cond.ObjectID, cond.PObjectID, cond.ObjectType, cond.ObjectTypeDesc, cond.SN, cond.IsValid);
                return DbManager.ExecNonQuery(cmd) > 0;
            }
            else if (cond.action == 4)
            {
                string _strsql = string.Format("update {0} set OBJ_NAME = :p_OBJ_NAME where OBJ_ID =:p_OBJ_ID", tablename);
                DbCmdHelper cmd = new DbCmdHelper(dbProvider, _strsql,
                    delegate()
                    {
                        return DbCmdHelper.CreateParameters(dbProvider, "OBJ_NAME", "OBJ_ID").ToArray();
                    });
                cmd.DbCmdInfo.CommandType = CommandType.Text;
                cmd.SetParamValues(cond.ObjectName, cond.ObjectID);
                return DbManager.ExecNonQuery(cmd) > 0;
            }


            return false;
        }




        /// <summary>
        /// 文件夹重命名
        /// </summary>
        /// <param name="cond"></param>
        /// <returns></returns>
        public bool Rename(TB_Tree_ObjectInfo model, string type)
        {
            string tablename = "";
            if (type == "person")
                tablename = "TB_TREE_PERSON";
            else if (type == "team")
                tablename = "TB_TREE_TEAM";

            StringBuilder strSql = new StringBuilder();

            string _strsql = string.Format("update {0} set OBJ_NAME = :p_OBJ_NAME where OBJ_ID =:p_OBJ_ID", tablename);
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, _strsql,
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider, "OBJ_NAME", "OBJ_ID").ToArray();
                });
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            cmd.SetParamValues(model.ObjectName, model.ObjectID);
            return DbManager.ExecNonQuery(cmd) > 0;




        }



        public List<TB_Tree_ObjectInfo> QueryTreeObjectByRoles(IEnumerable<string> roleIDList, TreeType treeType = TreeType.Target)
        {
            return null;
        }


        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="FileID">文件ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetTree_PersonBySql(string OBJ_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select t.*, t.rowid from tb_tree_person t where obj_id=" + OBJ_ID + "");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;

        }
        

        // <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="FileID">文件ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetTree_TeamBySql(string OBJ_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select t.*, t.rowid from tb_tree_team t where obj_id=" + OBJ_ID + "");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;

        }

        /// <summary>
        /// 个人文件夹剪切
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool UpdateTree_PersonCUTBySql(TB_Tree_ObjectInfo model)
        {
            string strSQL = @" UPDATE tb_tree_person 
		 set P_OBJ_ID = :p_P_OBJ_ID where OBJ_ID=:p_OBJ_ID";
            bool result = true;

            //新建一个数据库操作帮助类
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSQL,
                //参数生成
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider, "OBJ_ID", "P_OBJ_ID").ToArray();
                });
            //参数设值
            cmd.SetParamValues(model.ObjectID, model.PObjectID);
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            int back = DbManager.ExecNonQuery(cmd);
            if (back == 0)
                result = false;

            return result;

        }


        /// <summary>
        /// 课题组文件夹剪切
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool UpdateTree_TeamCUTBySql(TB_Tree_ObjectInfo model)
        {
            string strSQL = @" UPDATE tb_tree_team 
		 set P_OBJ_ID = :p_P_OBJ_ID where OBJ_ID=:p_OBJ_ID";
            bool result = true;

            //新建一个数据库操作帮助类
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSQL,
                //参数生成
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider, "OBJ_ID", "P_OBJ_ID").ToArray();
                });
            //参数设值
            cmd.SetParamValues(model.ObjectID, model.PObjectID);
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            int back = DbManager.ExecNonQuery(cmd);
            if (back == 0)
                result = false;

            return result;

        }

        /// <summary>
        /// 获取子文件夹文件列表
        /// </summary>
        /// <param name="FileID">文件ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetTree_PersonByP_ID(string OBJ_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select t.*, t.rowid from tb_tree_person t where P_OBJ_ID=" + OBJ_ID + "");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;

        }

        /// <summary>
        /// 获取子文件夹文件列表
        /// </summary>
        /// <param name="FileID">文件ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetTree_TeamByP_ID(string OBJ_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select t.*, t.rowid from tb_tree_Team t where P_OBJ_ID=" + OBJ_ID + "");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;

        }

        //删除文件夹
        public bool deleteTREE_PersonBySQL(string OBJ_ID)
        {
            bool result = true;

            string sql = @"delete from  tb_tree_person  where OBJ_ID=:p_OBJ_ID ";

            //新建一个数据库操作帮助类
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, sql,
                //参数生成
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider, "OBJ_ID").ToArray();
                });
            //参数设值
            cmd.SetParamValues(OBJ_ID);
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            int back = DbManager.ExecNonQuery(cmd);
            return result;
        }

        //删除文件夹
        public bool deleteTREE_TeamBySQL(string OBJ_ID)
        {
            bool result = true;

            string sql = @"delete from  tb_tree_team  where OBJ_ID=:p_OBJ_ID ";

            //新建一个数据库操作帮助类
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, sql,
                //参数生成
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider, "OBJ_ID").ToArray();
                });
            //参数设值
            cmd.SetParamValues(OBJ_ID);
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            int back = DbManager.ExecNonQuery(cmd);
            return result;
        }


        /// <summary>
        /// 个人新建文件夹
        /// </summary>
        /// <param name="cond"></param>
        /// <returns></returns>
        public bool PersonAddDir(TB_TreeObjectEditCond cond)
        {
            if (IsExist(cond, "person"))
            {
                return false;
            }
            StringBuilder strSql = new StringBuilder();

            strSql.Append("INSERT INTO TB_TREE_PERSON (OBJ_NAME ,OBJ_ID,P_OBJ_ID ,OBJ_TYPE ,OBJ_TYPE_DESC ,SN ,ISVALID,USER_ID) ");
            strSql.Append(" VALUES (:p_OBJ_NAME ,:p_OBJ_ID,:p_P_OBJ_ID ,:p_OBJ_TYPE ,:p_OBJ_TYPE_DESC,:p_SN,:p_ISVALID,:p_USER_ID)");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString(),
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider, "OBJ_NAME", "OBJ_ID", "P_OBJ_ID", "OBJ_TYPE", "OBJ_TYPE_DESC", "SN", "ISVALID", "USER_ID").ToArray();
                });
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            cmd.SetParamValues(cond.ObjectName, cond.ObjectID, cond.PObjectID, cond.ObjectType, cond.ObjectTypeDesc, cond.SN, cond.IsValid, cond.USER_ID);
            return DbManager.ExecNonQuery(cmd) > 0;


        }

        /// <summary>
        /// 课题组新建文件夹
        /// </summary>
        /// <param name="cond"></param>
        /// <returns></returns>
        public bool TeamAddDir(TB_TreeObjectEditCond cond)
        {
            if (IsExist(cond, "team"))
            {
                return false;
            }
            StringBuilder strSql = new StringBuilder();

            strSql.Append("INSERT INTO TB_TREE_TEAM (OBJ_NAME ,OBJ_ID,P_OBJ_ID ,OBJ_TYPE ,OBJ_TYPE_DESC ,SN ,ISVALID,USER_ID,TEAM_ID) ");
            strSql.Append(" VALUES (:p_OBJ_NAME ,:p_OBJ_ID,:p_P_OBJ_ID ,:p_OBJ_TYPE ,:p_OBJ_TYPE_DESC,:p_SN,:p_ISVALID,:p_USER_ID,:p_TEAM_ID)");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString(),
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider, "OBJ_NAME", "OBJ_ID", "P_OBJ_ID", "OBJ_TYPE", "OBJ_TYPE_DESC", "SN", "ISVALID", "USER_ID", "TEAM_ID").ToArray();
                });
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            cmd.SetParamValues(cond.ObjectName, cond.ObjectID, cond.PObjectID, cond.ObjectType, cond.ObjectTypeDesc, cond.SN, cond.IsValid, cond.USER_ID, cond.Team_ID);
            return DbManager.ExecNonQuery(cmd) > 0;


        }

        /// <summary>
        /// 判断是否有相同的文件夹名称
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        public bool IsExist(TB_TreeObjectEditCond cond,string foldertype)
        {
            string ObjectName = cond.ObjectName;
            string TEAM_ID = cond.Team_ID;
            string PObjectID =cond.PObjectID.ToString();

            StringBuilder strSql = new StringBuilder();
            if (foldertype == "team")
            {
                strSql.Append("SELECT count(*) FROM TB_TREE_TEAM WHERE  OBJ_NAME = '" + ObjectName + "' and P_OBJ_ID='" + PObjectID + "' and TEAM_ID='" + TEAM_ID + "'");
            }
            else if (foldertype == "person")
            {
                strSql.Append("SELECT count(*) FROM TB_TREE_PERSON WHERE OBJ_NAME = '" + ObjectName + "' and P_OBJ_ID='" + PObjectID + "'");
            }
            else if (foldertype == "xmk")
            {
                strSql.Append("SELECT count(*) FROM tb_tree_xmk WHERE OBJ_NAME = '" + ObjectName + "' and P_OBJ_ID='" + PObjectID + "'");
            }
            
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            object obj = DbManager.ExecScalar(cmd);

            return (Convert.ToInt16(obj) != 0);
        }


        /// <summary>
        /// 获取目标树的第一级
        /// </summary>
        /// <returns>目标树列表</returns>
        public DataTable GetTreeTargetOne()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select t.*, t.rowid from tb_tree_target t where p_obj_id=0 order by sn");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;

        }

        /// <summary>
        /// 获取通过父级ID目标树的子级
        /// </summary>
        /// <returns>目标树列表</returns>
        public DataTable GetSubTreeTarget(string P_OBJ_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select t.*, t.rowid from tb_tree_target t where p_obj_id=" + P_OBJ_ID + " order by sn");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;

        }


        public DataTable QuerySql(string sql)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(sql);
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;
        }

        /// <summary>
        /// 获取通过父级ID目标树的子级
        /// </summary>
        /// <returns>目标树列表</returns>
        public DataTable GetTargetNode(string OBJ_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select t.*, t.rowid from tb_tree_target t where obj_id=" + OBJ_ID + " order by sn");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;

        }






        /// <summary>
        /// 获取通过父级ID目标树的子级
        /// </summary>
        /// <returns>目标树列表</returns>
        public DataTable GetAllSubTreeTarget(string obj_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select t.*, t.rowid from tb_tree_target t start with  obj_id=" + obj_id + " connect by prior  obj_id =p_obj_id   order by sn");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;

        }

    

        public DataTable getSearchTarget(string target, string bus)
        {
            string strSql = " select t_eng_name,t_cn_name,t_field from tb_table_dict t where t_field is not null and  t_cat ='" + bus + "'";

            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql);
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            List<DataRow> drs = new List<DataRow>();
            foreach (DataRow dr in ds.Rows)
            {
                strSql = "select * from " + dr["t_eng_name"] + " where " + dr["t_field"].ToString() + "='" + target + "'";
                DbCmdHelper cmd1 = new DbCmdHelper(dbProvider, strSql);
                cmd1.DbCmdInfo.CommandType = CommandType.Text;
                DataTable dt = DbHelper.ExecFullDataTable(cmd1.DbProvider, cmd1.DbCmdInfo);
                int rowCount = dt.Rows.Count;
                if (rowCount < 1)
                    drs.Add(dr);
                else
                {
                    dr["t_field"] = rowCount.ToString();
                }
            }

            foreach (DataRow dr in drs)
            {
                ds.Rows.Remove(dr);
            }


            if (ds != null)
            {
                return ds.Copy();
            }
            return null;
        }


        public DataTable getTableData(string tableName, string target)
        {
            string sql = "select t_field from tb_table_dict where t_eng_name='" + tableName + "'";
            DataTable dtRes = null;// = new DataTable();
            StringBuilder strSql = new StringBuilder();
            strSql.Append(sql);
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds.Rows.Count > 0)
            {
                string field = ds.Rows[0][0].ToString();

                sql = "select * from " + tableName + " where " + field + "= '" + target + "'";
                DbCmdHelper cmd1 = new DbCmdHelper(dbProvider, sql);
                cmd1.DbCmdInfo.CommandType = CommandType.Text;
                dtRes = DbHelper.ExecFullDataTable(cmd1.DbProvider, cmd1.DbCmdInfo);

            }

            if (dtRes != null)
            {
                return dtRes.Copy();
            }
            return null;
        }
        /// <summary>
        /// 查询统计资产数据
        /// </summary>
        public DataTable QueryGridData(string strId)
        {
            DbCmdHelper cmd = new DbCmdHelper(dbProvider,
                "sp_AssetsStatistics",
                 delegate()
                 {
                     return DbCmdHelper.CreateParameters(dbProvider,
                         "parentId").ToArray();
                 }
              );
            //参数设值
            // cmd.SetParamValues();
            cmd.SetParamValues(strId);
            cmd.DbCmdInfo.CommandType = CommandType.StoredProcedure;
            return DbHelper.ExecFullDataTable(dbProvider, cmd.DbCmdInfo);
        }

        /// <summary>
        /// 查询业务类型
        /// </summary>
        public DataTable QueryBus()
        {
            DbCmdHelper cmd = new DbCmdHelper(dbProvider,
                "select t_cat from tb_table_dict where t_field is not null   group by t_cat"
              );
            //参数设值
            // cmd.SetParamValues();

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            return DbHelper.ExecFullDataTable(dbProvider, cmd.DbCmdInfo);
        }

        /// <summary>
        /// 查询所有目标节点
        /// </summary>
        public DataTable QueryBusData(string parentId)
        {
            DbCmdHelper cmd = new DbCmdHelper(dbProvider,
                "sp_query_target",
                 delegate()
                 {
                     return DbCmdHelper.CreateParameters(dbProvider,
                         "parentId").ToArray();
                 }
              );
            //参数设值
            // cmd.SetParamValues();
            cmd.SetParamValues(parentId);
            cmd.DbCmdInfo.CommandType = CommandType.StoredProcedure;
            return DbHelper.ExecFullDataTable(dbProvider, cmd.DbCmdInfo);
        }
        /// <summary>
        /// 查询所有目标节点
        /// </summary>
        public DataTable GetAllTreeTarget()
        {
            DbCmdHelper cmd = new DbCmdHelper(dbProvider,
                "select * from tb_tree_target"
              );

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            return DbHelper.ExecFullDataTable(dbProvider, cmd.DbCmdInfo);
        }

        /// <summary>
        /// 查询所有业务节点
        /// </summary>
        public DataTable GetAllTreeBiz()
        {
            DbCmdHelper cmd = new DbCmdHelper(dbProvider,
                "select * from tb_tree_bus"
              );

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            return DbHelper.ExecFullDataTable(dbProvider, cmd.DbCmdInfo);
        }



    }
}
